<?php

namespace catchAdmin\basics\controller;

use catcher\base\CatchRequest as Request;
use catcher\CatchAuth;
use catcher\CatchResponse;
use catcher\base\CatchController;
use catchAdmin\basics\model\ExpenseType as ExpenseTypeModel;
use catchAdmin\basics\request\ExpenseTypeCreateRequest;
class ExpenseType extends CatchController
{
    
    protected $expenseTypeModel;
    
    /**
     *
     * @time 2022/03/03 11:45
     * @param ExpenseTypeModel $expenseTypeModel
     * @return mixed
     */
    public function __construct(ExpenseTypeModel $expenseTypeModel)
    {
        $this->expenseTypeModel = $expenseTypeModel;
    }
    
    /**
     * 列表
     * @time 2022/03/03 11:45
     * @throws \think\db\exception\DbException
     * @return \think\response\Json
     */
    public function index(CatchAuth $auth)
    {
        $user=$auth->user();
        return CatchResponse::paginate($this->expenseTypeModel->getList($user['tenant_id']));
    }

    /**
     * 新建
     * @param CatchAuth $auth
     * @param ExpenseTypeCreateRequest $request
     * @return \think\response\Json
     */
    public function create(CatchAuth $auth,ExpenseTypeCreateRequest $request)
    {
        $params = $request->filterEmptyField()->post();
        $user = $auth->user();
        $params['tenant_id'] = $user['tenant_id'];
        return CatchResponse::success($this->expenseTypeModel->storeBy($params),'新建成功');
    }
    
    /**
     * 更新
     * @time 2022/03/03 11:45
     * @param ExpenseTypeCreateRequest $request
     * @return \think\response\Json
     */
    public function update(ExpenseTypeCreateRequest $request)
    {
        $params = $request->filterEmptyField()->post();
        return CatchResponse::success($this->expenseTypeModel->updateBy($params['id'], $params),'更新成功');
    }
    
    /**
     * 删除
     * @time 2022/03/03 11:45
     * @throws \think\db\exception\DbException
     * @return \think\response\Json
     */
    public function delete(Request $request)
    {
        $id = $request->post('id');
        return CatchResponse::success( $this->expenseTypeModel->deleteBy($id),'删除成功');
    }
     /**
     * 查询全部费用
     * @time 2022/03/03 11:45
     * @throws \think\db\exception\DbException
     * @return \think\response\Json
     */
    public function getExpenseType()
    {
        $data = $this->expenseTypeModel->where('deleted_at',0)->field('code,name')->select();
        return CatchResponse::success($data,'查询成功');
    }
}